package com.zzx.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzx.boot.common.CustomException;
import com.zzx.boot.common.R;
import com.zzx.boot.dto.SetmealDto;
import com.zzx.boot.mapper.SetmealMapper;
import com.zzx.boot.pojo.Category;
import com.zzx.boot.pojo.Setmeal;
import com.zzx.boot.pojo.SetmealDish;
import com.zzx.boot.service.CategoryService;
import com.zzx.boot.service.SetmealDishService;
import com.zzx.boot.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Resource
    //套餐
    private SetmealService setmealService;
    @Resource
    //套餐菜
    private SetmealDishService setmealDishService;
    @Resource
    //套餐分类
    private CategoryService categoryService;

    @Override
    @Transactional
    public R<String> saveSetmeal(SetmealDto setmealDto) {

        //添加套餐
        boolean save = setmealService.save(setmealDto);
        if (!save){
            throw new CustomException("添加套餐失败!");
        }
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }

        /*添加套餐菜*/
        boolean b = setmealDishService.saveBatch(setmealDishes);
        if (!b){
            throw new CustomException("添加套餐菜失败!");
        }
        return R.success("添加套餐成功!");
    }

    @Override
    public R<Page<SetmealDto>> pageAndSetmeal(Integer page, Integer pageSize, String name) {
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getPrice);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        /*查询到套餐信息的*/
        Page<Setmeal> page1 = setmealService.page(setmealPage, queryWrapper);
        //拷贝
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        /*但是没有分类信息*/
        //查询分类信息
        List<Setmeal> records = page1.getRecords();
        List<SetmealDto> list1 = new ArrayList();
        for (Setmeal record : records) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record,setmealDto);
            Category byId = categoryService.getById(record.getCategoryId());
            if (byId!=null){
                setmealDto.setCategoryName(byId.getName());
            }
            list1.add(setmealDto);
        }
        setmealDtoPage.setRecords(list1);

        return R.success(setmealDtoPage);
    }

    @Override
    @Transactional
    public R<SetmealDto> updateAndById(Long id) {
        //查询套餐信息
        Setmeal byId = setmealService.getById(id);
        if (byId==null){
            throw new CustomException("查询套餐失败!");
        }
        //查询分类信息
        Category byId1 = categoryService.getById(byId.getCategoryId());
        if (byId1==null){
            throw new CustomException("查询分类失败");
        }
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        //查询菜品
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(byId,setmealDto);
        setmealDto.setCategoryName(byId1.getName());
        setmealDto.setSetmealDishes(list);
        return R.success(setmealDto);
    }

    @Override
    @Transactional
    public R<String> saveUpdate(SetmealDto setmealDto) {
        //修改套餐
        boolean b = setmealService.updateById(setmealDto);
        if (!b){
            throw new CustomException("套餐修改失败!");
        }
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        //删除所有的菜
        boolean remove = setmealDishService.remove(queryWrapper);
        if (!remove) {
            throw new CustomException("删除套餐菜失败");
        }
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        //添加所有的菜
        boolean b1 = setmealDishService.saveBatch(setmealDishes);
        if (!b1){
            throw new CustomException("修改套餐菜失败!");
        }
        return R.success("修改成功");
    }


}
